package com.example.hp.storeapp.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.example.hp.storeapp.R;
import com.example.hp.storeapp.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class FavorFragment extends BaseFragment {
    private RadioButton shopRadio;
    private RadioButton cookRadio;
    private ShopFavoritesFragment shopFavoritesFragment;
    private CookFavoritesFragment cookFavoritesFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;


    public FavorFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        shopRadio = (RadioButton) view.findViewById(R.id.radio_shop);
        cookRadio = (RadioButton) view.findViewById(R.id.radio_food);


        shopRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    shopFavoritesFragment = new ShopFavoritesFragment();
                    replaceFragment(shopFavoritesFragment);

                }
            }
        });

        cookRadio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    cookFavoritesFragment = new CookFavoritesFragment();
                    replaceFragment(cookFavoritesFragment);

                }
            }
        });

        fragmentManager = getChildFragmentManager();
        shopRadio.setChecked(true);
    }

    private void replaceFragment(Fragment fragment){
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder,fragment);
        transaction.commit();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_favor, container, false);
    }

}
